package com.engine.view;

import java.util.InputMismatchException;
import java.util.Scanner;
import com.engine.pattern.pawn.Pawn;
import com.engine.pattern.player.Player;
import com.ressource.ColoredString;

/**
 * @author jonathan
 * @author fabienne
 */
public final class DisplayColored
{

	/**
	 * 
	 */
	public DisplayColored()
	{

	}

	/**
	 * @return
	 */
	public String askPlayerName()
	{
		Scanner scan = new Scanner(System.in);
		ColoredString nameColored = new ColoredString();

		System.out.println("Player " + Player.getPlayerFn()
				+ " enter player name: ");
		String name;
		name = scan.nextLine();

		nameColored.setOriginalString(name);
		nameColored.cyan();
		System.out.println("Your name: " + nameColored);

		return name;

	}

	/**
	 * @return
	 */
	public Integer askPlayerTeam()
	{
		Scanner scan = new Scanner(System.in);
		ColoredString teamColored = new ColoredString();
		Integer team = 0;

		System.out.println("Player " + Player.getPlayerFn()
				+ " enter your team number: ");
		try
		{
			team = scan.nextInt();
			teamColored.setOriginalString(team);
			teamColored.cyan();
			System.out.println("Your team number: " + teamColored);
		}
		catch (NumberFormatException e)
		{
			team = null;
		}
		catch (InputMismatchException e)
		{
			team = null;
		}

		return team;
	}

	/**
	 * @param name
	 * @param score
	 */
	public void printPlayerScore(String name, Integer score)
	{
		ColoredString nameColored = new ColoredString(name);
		ColoredString scoreColored = new ColoredString(score);
		String message = new String();

		nameColored.cyan();
		scoreColored.yellow();

		message = (score > 1) ? "The player\t" + nameColored + "\thas\t"
				+ scoreColored + "\tpoint" : "The player\t" + nameColored
				+ "has\t" + scoreColored + "\tpoints";
		System.out.println(message);
	}

	/**
	 * @return
	 */
	public Integer askHowManyHuman()
	{
		Scanner scan = new Scanner(System.in);
		Integer myChoice = null;
		ColoredString myChoiceColored = new ColoredString("");
		ColoredString human = new ColoredString("human");

		human.cyan();
		System.out.println("How many " + human + " player for this round?");
		try
		{
			myChoice = scan.nextInt();
			myChoiceColored.setOriginalString(myChoice);
			myChoiceColored.cyan();
			System.out.println("Your choice: " + myChoiceColored);
		}
		catch (NumberFormatException e)
		{
			myChoice = null;
		}
		catch (InputMismatchException e)
		{
			myChoice = null;
		}

		return myChoice;

	}

	/**
	 * @return
	 */
	public Integer askHowManyComputer()
	{
		Scanner scan = new Scanner(System.in);
		Integer myChoice = 0;
		ColoredString myChoiceColored = new ColoredString(myChoice);
		ColoredString computer = new ColoredString("computer");

		computer.yellow();
		System.out.println("How many " + computer + " player for this round?");
		try
		{
			myChoice = scan.nextInt();
			myChoiceColored.setOriginalString(myChoice);
			myChoiceColored.cyan();
			System.out.println("Your choice: " + myChoiceColored);
		}
		catch (NumberFormatException e)
		{
			myChoice = null;
		}
		catch (InputMismatchException e)
		{
			myChoice = null;
		}

		return myChoice;

	}

	/**
	 * 
	 */
	public void noPlayer()
	{
		ColoredString myWarningColored = new ColoredString(
				"There is no player for this round !!!");
		myWarningColored.red();
		System.out.println(myWarningColored);
	}

	/**
	 * 
	 */
	public void invalidChoice()
	{
		ColoredString message = new ColoredString("Invalid choice retry again");
		message.red();
		System.out.println(message);
	}

	/**
	 * 
	 */
	public void listWinnerScore()
	{
		System.out.println("Winner list:");
	}

	/**
	 * 
	 */
	public void listScore()
	{
		System.out.println("Player list:");
	}

	/**
	 * @param name
	 * @param teamNumber
	 * @param score
	 */
	public void printScore(String name, String teamNumber, Integer score)
	{
		ColoredString nameColored = new ColoredString(name);
		ColoredString teamColored = new ColoredString(teamNumber);
		ColoredString scoreColored = new ColoredString(score);
		nameColored.cyan();
		teamColored.red();
		scoreColored.yellow();
		if (score > 1)
		{
			System.out.println("\t-Player " + nameColored + " team "
					+ teamColored + " has " + scoreColored + " points");
		}
		else
		{
			System.out.println("\t-Player " + nameColored + " team "
					+ teamColored + " has " + scoreColored + " point");
		}
	}

	/**
	 * @param name
	 * @param teamNumber
	 * @param score
	 */
	public void printScore(String name, Integer teamNumber, Integer score)
	{
		ColoredString nameColored = new ColoredString(name);
		ColoredString teamColored = new ColoredString(Integer
				.toString(teamNumber));
		ColoredString scoreColored = new ColoredString(score);
		nameColored.cyan();
		teamColored.red();
		scoreColored.yellow();
		if (score > 1)
		{
			System.out.println("\t-Player " + nameColored + " team "
					+ teamColored + " has " + scoreColored + " points");
		}
		else
		{
			System.out.println("\t-Player " + nameColored + " team "
					+ teamColored + " has " + scoreColored + " point");
		}
	}

	/**
	 * @param name
	 * @param pawn
	 * @param axisName
	 * @param playerId
	 * @return
	 */
	public Integer askAxis(String name, String pawn, String axisName,
			Integer playerId)
	{
		ColoredString nameColored = new ColoredString(name);
		ColoredString pawnColored = new ColoredString(pawn);
		ColoredString axisNameColored = new ColoredString(axisName);
		Integer axis = null;
		Scanner scan = new Scanner(System.in);

		switch (playerId)
		{
			case 1:
			{
				nameColored.magenta();
				break;
			}
			case 2:
			{
				nameColored.cyan();
				break;
			}
			case 3:
			{
				nameColored.yellow();
				break;
			}
			case 4:
			{
				nameColored.red();
				break;
			}
			default:
			{
				nameColored.lightGrey();
				break;
			}
		}
		pawnColored.red();
		axisNameColored.red();

		System.out.println(nameColored + ", where do you want to put your "
				+ pawnColored + ", please select a " + axisNameColored
				+ " axis");
		try
		{
			axis = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis = null;
		}
		catch (InputMismatchException e)
		{
			axis = null;
		}

		return axis;
	}

	/**
	 * @param name
	 * @param pawn
	 * @param playerId
	 * @return
	 */
	public Integer[] askWhichPawnMove(String name, String pawn, Integer playerId)
	{
		ColoredString nameColored = new ColoredString(name);
		ColoredString pawnColored = new ColoredString(pawn.substring(24));
		Integer[] axis = new Integer[2];
		Scanner scan = new Scanner(System.in);

		switch (playerId)
		{
			case 1:
			{
				nameColored.magenta();
				break;
			}
			case 2:
			{
				nameColored.cyan();
				break;
			}
			case 3:
			{
				nameColored.yellow();
				break;
			}
			case 4:
			{
				nameColored.red();
				break;
			}
			default:
			{
				nameColored.lightGrey();
				break;
			}
		}
		pawnColored.red();

		System.out.println(nameColored + " please select a " + pawnColored
				+ ":");
		System.out.print("X axis: ");
		try
		{
			axis[1] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[1] = null;
		}
		catch (InputMismatchException e)
		{
			axis[1] = null;
		}
		System.out.print("Y axis: ");
		try
		{
			axis[0] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[0] = null;
		}
		catch (InputMismatchException e)
		{
			axis[0] = null;
		}

		return axis;
	}

	/**
	 * @param name
	 * @param playerId
	 * @return
	 */
	public Integer[] askWhichPawnMove(String name, Integer playerId)
	{
		ColoredString nameColored = new ColoredString(name);
		Integer[] axis = new Integer[2];
		Scanner scan = new Scanner(System.in);

		switch (playerId)
		{
			case 1:
			{
				nameColored.magenta();
				break;
			}
			case 2:
			{
				nameColored.cyan();
				break;
			}
			case 3:
			{
				nameColored.yellow();
				break;
			}
			case 4:
			{
				nameColored.red();
				break;
			}
			default:
			{
				nameColored.lightGrey();
				break;
			}
		}

		System.out.println(nameColored + " please select a pawn:");
		System.out.print("X axis: ");
		try
		{
			axis[1] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[1] = null;
		}
		catch (InputMismatchException e)
		{
			axis[1] = null;
		}
		System.out.print("Y axis: ");
		try
		{
			axis[0] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[0] = null;
		}
		catch (InputMismatchException e)
		{
			axis[0] = null;
		}

		return axis;
	}

	/**
	 * @param name
	 * @param playerId
	 * @param pawnType
	 * @return
	 */
	public Integer[] askWhichPawnMove(String name, Integer playerId,
			String pawnType)
	{
		ColoredString nameColored = new ColoredString(name);
		Integer[] axis = new Integer[2];
		Scanner scan = new Scanner(System.in);

		switch (playerId)
		{
			case 1:
			{
				nameColored.magenta();
				break;
			}
			case 2:
			{
				nameColored.cyan();
				break;
			}
			case 3:
			{
				nameColored.yellow();
				break;
			}
			case 4:
			{
				nameColored.red();
				break;
			}
			default:
			{
				nameColored.lightGrey();
				break;
			}
		}

		System.out.println(nameColored + " please select a " + pawnType + ":");
		System.out.print("X axis: ");
		try
		{
			axis[1] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[1] = null;
		}
		catch (InputMismatchException e)
		{
			axis[1] = null;
		}
		System.out.print("Y axis: ");
		try
		{
			axis[0] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[0] = null;
		}
		catch (InputMismatchException e)
		{
			axis[0] = null;
		}

		return axis;
	}

	/**
	 * @param name
	 * @param pawn
	 * @param playerId
	 * @return
	 */
	public Integer[] askWherePawnMove(String name, String pawn, Integer playerId)
	{
		ColoredString nameColored = new ColoredString(name);
		ColoredString pawnColored = new ColoredString(pawn);
		Integer[] axis = new Integer[2];
		Scanner scan = new Scanner(System.in);

		switch (playerId)
		{
			case 1:
			{
				nameColored.magenta();
				break;
			}
			case 2:
			{
				nameColored.cyan();
				break;
			}
			case 3:
			{
				nameColored.yellow();
				break;
			}
			case 4:
			{
				nameColored.red();
				break;
			}
			default:
			{
				nameColored.lightGrey();
				break;
			}
		}
		pawnColored.red();

		System.out.println(nameColored + " please select a coordinate for "
				+ pawnColored + ":");
		System.out.print("X axis: ");
		try
		{
			axis[1] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[1] = null;
		}
		catch (InputMismatchException e)
		{
			axis[1] = null;
		}
		System.out.print("Y axis: ");
		try
		{
			axis[0] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[0] = null;
		}
		catch (InputMismatchException e)
		{
			axis[0] = null;
		}

		return axis;
	}

	/**
	 * @param name
	 * @param playerId
	 * @return
	 */
	public Integer[] askWherePawnMove(String name, Integer playerId)
	{
		ColoredString nameColored = new ColoredString(name);
		Integer[] axis = new Integer[2];
		Scanner scan = new Scanner(System.in);

		switch (playerId)
		{
			case 1:
			{
				nameColored.magenta();
				break;
			}
			case 2:
			{
				nameColored.cyan();
				break;
			}
			case 3:
			{
				nameColored.yellow();
				break;
			}
			case 4:
			{
				nameColored.red();
				break;
			}
			default:
			{
				nameColored.lightGrey();
				break;
			}
		}

		System.out.println(nameColored
				+ " please select a coordinate for pawn:");
		System.out.print("X axis: ");

		try
		{
			axis[1] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[1] = null;
		}
		catch (InputMismatchException e)
		{
			axis[1] = null;
		}
		System.out.print("Y axis: ");
		try
		{
			axis[0] = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			axis[0] = null;
		}
		catch (InputMismatchException e)
		{
			axis[0] = null;
		}

		return axis;
	}

	/**
	 * 
	 */
	public void badPlayerNb()
	{
		ColoredString warning = new ColoredString(
				"You cannot create more than 4 players ....\nRetry again");
		warning.red();
		System.out.println(warning);
	}

	/**
	 * @param moveFn
	 * @return
	 */
	public Integer chooseAnAction(Integer moveFn)
	{
		Integer choice = null;
		System.out.println("You have " + moveFn + " move left");
		System.out.println("Would you like to :");
		System.out.println("1- Move your pawn");
		System.out.println("2- Stop movement");
		Scanner scan = new Scanner(System.in);
		try
		{
			choice = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			choice = null;
		}
		catch (InputMismatchException e)
		{
			choice = null;
		}

		return choice;
	}

	/**
	 * @param c1
	 */
	public void cellEmpty(Integer[] c1)
	{
		Integer xAxis = c1[0];
		Integer yAxis = c1[1];
		ColoredString xAxisColored = new ColoredString(xAxis);
		ColoredString yAxisColored = new ColoredString(yAxis);
		xAxisColored.yellow();
		yAxisColored.yellow();

		System.out.println("There is no pawn at " + xAxisColored + "x"
				+ yAxisColored);
		System.out.println("Retry again");

	}

	/**
	 * 
	 */
	public void clear()
	{
		System.out.print("\033[H\033[2J");
	}

	/**
	 * @param grid
	 */
	public void show(Pawn[][] grid)
	{
		Integer xNumber = 0;
		Integer line = 0;
		Integer column = 0;
		ColoredString pawnIdColored = new ColoredString();
		// clear screen
		clear();
		System.out.print("  ");
		// print number 0 to 29 on one line
		for (xNumber = 0; xNumber < grid.length; xNumber++)
		{
			if (xNumber < 10)
			{
				System.out.print(" " + xNumber + " ");
			}
			else
			{
				System.out.print(" " + xNumber);
			}
		}

		System.out.print("\n ");
		for (line = 0; line < grid.length; line++)
		{
			for (Integer numberOfUnderscore = 0; numberOfUnderscore < 91; numberOfUnderscore++)
			{
				System.out.print("─");
			}
			if (line < 10)
			{
				System.out.print("\n " + line + "│");
			}
			else
			{
				System.out.print("\n" + line + "│");
			}
			for (column = 0; column < grid[line].length; column++)
			{
				if (grid[line][column] == null)
				{
					pawnIdColored.setOriginalString("  ");
				}
				else
				{
					Pawn pawn = grid[line][column];
					char pawnId1 = pawn.getClass().getName().charAt(24);
					char pawnId2 = pawn.getClass().getName().charAt(25);
					pawnIdColored.setOriginalString(Character.toString(pawnId1)
							+ Character.toString(pawnId2));
					switch (pawn.getPlayerId())
					{
						case 1:
						{
							pawnIdColored.magenta();
							break;
						}
						case 2:
						{
							pawnIdColored.cyan();
							break;
						}
						case 3:
						{
							pawnIdColored.yellow();
							break;
						}
						case 4:
						{
							pawnIdColored.red();
							break;
						}
						default:
						{
							pawnIdColored.lightGrey();
							break;
						}
					}
				}

				System.out.print(pawnIdColored + "│");
			}
			System.out.print(" " + line + "\n  ");
		}
		for (Integer numberOfUnderscore = 0; numberOfUnderscore < 91; numberOfUnderscore++)
		{
			System.out.print("─");
		}
		System.out.print("\n  ");
		// print number 0 to 29 on one line
		for (xNumber = 0; xNumber < grid.length; xNumber++)
		{
			if (xNumber < 10)
			{
				System.out.print(" " + xNumber + " ");
			}
			else
			{
				System.out.print(" " + xNumber);
			}
		}
		System.out.println();
	}

	/**
	 * @return
	 */
	public Integer askWhichMode()
	{
		Scanner scan = new Scanner(System.in);
		Integer mode = 0;

		System.out.println("Choose game mode");
		System.out
				.println("1 - Use a random method to place all pawns on board");
		System.out
				.println("2 - Player will place his pawns himself on the board");
		try
		{
			mode = scan.nextInt();
		}
		catch (NumberFormatException e)
		{
			mode = 0;
		}
		catch (InputMismatchException e)
		{
			mode = 0;
		}
		return mode;
	}

	/**
	 * 
	 */
	public void invalidProt()
	{
		System.out
				.println("The selected Proteine cannot capture another Metabolite");

	}

}
